package ddd.ajava.chapter5.question1.tcp;
import java.net.*;
import java.io.*;
import java.util.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class TcpFileClient {//一对一的一个客服端
	static File file;
	static Scanner input=new Scanner(System.in);
	public static void main(String[] args) throws UnknownHostException, IOException{
	   Socket client=new Socket("127.0.0.1",7878);	
	   boolean cycle=true;
	   
	   //输入输出流(接受服务器)
	   BufferedReader read=new BufferedReader(new InputStreamReader(client.getInputStream()));
	   PrintStream write=new PrintStream(client.getOutputStream());
	   //从文件从得到输出流
	   FileInputStream readFile=null;
	   
	   System.out.print("请输入File:");
	   String fileName=input.next();
	   file=new File(fileName);
	   
	   input=new Scanner(file);//从文件中得到输出
	   		   
	   //从文件从得到输出流
	   readFile=new FileInputStream(file);
	   
	   String getName=file.getName();//得到文件的名字
	   write.println(getName);
	   
	  while(input.hasNext()){
		  write.print(input.next());
	  }
	   //write.flush();
	   
	  String response=read.readLine();//从服务端得到相应
	  System.out.println(response);
	   //相应的流的关闭
	   write.close();
	   read.close();
	   readFile.close();
	   client.close();
   }
	public static void FileInformation(String texts){//显示收到的文本
		JFrame showInfo=new JFrame();
		JPanel jp=new JPanel();
		JTextArea text=new JTextArea(20,20);//文本区
		text.append(texts);//输入
		jp.add(text);
		showInfo.add(jp);
		
		showInfo.setTitle("服务端相应！");
		showInfo.setVisible(true);
		showInfo.setSize(300,100);
		showInfo.setLocation(500, 500);
		showInfo.setDefaultCloseOperation(1);
	}
}
